package com.jt.www.job.issue;

import com.jt.www.biz.pay.service.PayUrlInfoService;
import com.jt.www.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * 同步员工账户基本信息定时任务
 */
@Component
@EnableScheduling
public class CheckIssueYNXPXScheduleTask implements SchedulingConfigurer {

    public static final Logger log = LoggerFactory.getLogger(CheckIssueYNXPXScheduleTask.class);
    private static final String MESSAGE_LOCK_KEY = "EDU_YNXPX_ISSUE";//锁前缀
    @Autowired
    private PayUrlInfoService payUrlInfoService;
    /**
     * 任务开关
     */
    @Value("${jtpf.ynxpxissue.open}")
    private int open;

    /**
     * 任务执行周期
     */
    @Value("${jtpf.ynxpxissue.cron}")
    private String cron;

    /**
     * 最大请求次数
     */
    @Value("${jtpf.ynxpxissue.times}")
    private Integer times;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (open != 1) {
            return;
        }
        // 如果一个定时任务在执行，那么下一个定时不能执行
        if (RedisUtil.setOneHoursLock(MESSAGE_LOCK_KEY)) {
            Runnable task0 = () ->
                    payUrlInfoService.ynxpxIssueEffect(times);
            Trigger trigger0 = triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext);
            taskRegistrar.addTriggerTask(task0, trigger0);
        } else {
            log.info("云南学平险调用平安承保接口任务==>当天任务已经执行，不在重复执行");
        }
    }

}